package org.deri.socializer.com.xmpp;

import java.util.ArrayList;
import java.util.Iterator;

import org.deri.socializer.util.XMPPUtilities;
import org.jivesoftware.smack.packet.Message;

public class XmppConversationHistory {

	XmppChat xmppChat;
	ArrayList<Message> messages = new ArrayList<Message>();
	int maxConversationLength = -1;
	
	public XmppConversationHistory(XmppChat xmppChat) {
		this.xmppChat = xmppChat;
		this.maxConversationLength = -1;
	}
	
	public XmppConversationHistory(XmppChat xmppChat, int maxConversationsLength) {
		this.xmppChat = xmppChat;
		this.maxConversationLength = maxConversationsLength;
	}
	
	
	public ArrayList<Message> getMessages() {
		return this.messages;
	}
	
	
	public void addMessage(Message message) {
		this.messages.add(message);
		if (this.maxConversationLength > 0) {
			while (this.messages.size() > this.maxConversationLength) {
				this.messages.remove(0);
			}
		}
	}
	
	
	//"<b><font color='#000000'>" + sender.getUser() + ": </font></b> <font color='#000000'>" + message + "</font><br />"
	
	public String toHtmlString() {
		String html = "";
		Iterator<Message> messageIter = this.messages.iterator();
		Message msg;
		while (messageIter.hasNext()) {
			msg = messageIter.next();
			String sender = "";
			System.out.println("[toHtmlString] " + msg.getFrom());
			if (this.xmppChat instanceof XmppGroupChat)
				sender = XMPPUtilities.getUser(msg.getFrom());
			else if (this.xmppChat instanceof XmppPrivateChat)
				sender = XMPPUtilities.getSenderName(msg.getFrom());
			else
				sender = "unknown";
			html += "<b><font color='#000000'>" + sender + ": </font></b> <font color='#000000'>" + msg.getBody() + "</font><br />";
		}
		return html;
	}
	
	
}
